//
//  MYGChatTemplateListCell.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/20.
//

import UIKit

class MYGChatTemplateListCell: UITableViewCell {
    // MARK: - Property
    var model: MYGChatTemplateCellModel? {
        didSet {
            guard let model = model else {
                return
            }
            
            contentLabel.text = model.name
        }
    }
    // MARK: - init
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - UI
    private func configUI() {
        contentView.addSubview(contentLabel)
        contentView.addSubview(editButton)
        contentView.addSubview(deleteButton)
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            make.right.equalTo(editButton.snp.left).offset(-4)
            make.top.bottom.equalToSuperview().inset(10)
        }
        
        editButton.snp.makeConstraints { make in
            make.right.equalTo(deleteButton.snp.left).offset(-11)
            make.centerY.equalToSuperview()
        }
        
        deleteButton.snp.makeConstraints { make in
            make.right.equalToSuperview().inset(20)
            make.centerY.equalToSuperview()
        }
        
        editButton.setContentHuggingPriority(.required, for: .horizontal)
        editButton.setContentCompressionResistancePriority(.required, for: .horizontal)
        deleteButton.setContentHuggingPriority(.required, for: .horizontal)
        deleteButton.setContentCompressionResistancePriority(.required, for: .horizontal)
    }
    // MARK: - lazy load
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(15)
        label.textColor = .rgba(91, 91, 91, 1)
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var editButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "Chat/Menu/edit"), for: .normal)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        return btn
    }()
    
    private lazy var deleteButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "Chat/Menu/delete"), for: .normal)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)

        return btn
    }()
    
    @objc
    private func clickButton(_ btn: UIButton) {
        if btn == editButton {
            self.model?.editName?()
        } else if btn == deleteButton {
            self.model?.delete?()
        }
    }
}
